home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / sgistl.asc < prev    next >
Text File  |  1997-06-20  |  2KB  |  77 lines

  1. _The SGI Standard Template Library_
  2. by Matthew H. Austern 
  3.  
  4. Listing One
  5. int* find1(int* first, int* last, int value) 
  6.    while (first != last && *first != value) 
  7.       ++first; 
  8.    return first; 
  9.  
  10. Listing Two
  11. template <class InputIterator, class T> 
  12. InputIterator find(InputIterator first, InputIterator last, const T& value) 
  13.  while (first != last && *first != value) 
  14.    ++first; 
  15.  return first; 
  16.  
  17.  
  18. Listing Three 
  19. template <class InputIterator, class Predicate> 
  20. InputIterator find_if(InputIterato r first,InputIterator last,Predicate pred) 
  21.    while (first != last && !pred(*first)) 
  22.        ++first; 
  23.    return first; 
  24.  
  25. Listing Four
  26. #include <hash_map.h> 
  27. #include <stdio.h> 
  28. struct eqstr { 
  29.    bool operator()(const char* s1, const char* s2) const { 
  30.      return strcmp(s1, s2) == 0; 
  31.    } 
  32. }; 
  33. int main() 
  34.    hash_map<char*, int, hash<char*>, eqstr> days; 
  35.    days["January"] = 31; 
  36.    days["February"] = 28; 
  37.    days["March"] = 31; 
  38.    days["April"] = 30; 
  39.    days["May"] = 31; 
  40.    days["June"] = 30; 
  41.    printf("%s has %d days\n", "March", days["March"]); 
  42.    return 0; 
  43.  
  44. Listing Five 
  45. iterator insert(iterator position, const T& x) { 
  46.   link_type tmp = get_node(); 
  47.   construct(&((*tmp ).dat a), x); 
  48.   (*tmp).next = position.node; 
  49.   (*tmp).prev = (*position.node).prev; 
  50.   (*(link_type((*po sitio n.nod e).pr ev))).nex t = tmp; 
  51.   (*position.node). prev = tmp; 
  52.   ++length; 
  53.   return tmp; 
  54.  
  55. Listing Six
  56. link_type get_node() { 
  57.   link_type tmp = free_list; 
  58.   return free_list 
  59.       ? (free_list = (link_type)(free_l ist- >next ), tmp) 
  60.       : (next_avail == last ? (add_new_buffer(), next_avail++) 
  61.                               : next_avail++); } 
  62.  
  63. Listing Seven
  64. link_type get_node() { 
  65.    return list_node_alloc ator: :allocate( ); 
  66.  
  67.